02. Java Bytecode

Java Bytecode

ND079 JPND C3 L1 A02 Java Bytecode V3

What is Java Bytecode?

Java Bytecode is like assembly language for the JVM. The Java compiler takes all of our Java source code and turns it into simple statements that are easier for the JVM to translate.

Bytecode Example

 public static main([Ljava/lang/String;)V
   L0
    LINENUMBER 5 L0
    GETSTATIC java/lang/System.out : Ljava/io/PrintStream;
    LDC "Hello World"
    INVOKEVIRTUAL java/io/PrintStream.println

Bytecode is contained in files that have the .class extension. That’s because each java class you write will create a separate file of bytecode!

We can compile a .java file into a .class file using:

$ javac NameOfFile.java

Code

For example, if the below code was contained in a file called Test.java, you could compile it into Test.class by using the command:

javac Test.java

public class Test {
    public static void main(String[] args) {
        System.out.println("Hello World");
    }
}